/*
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

OAA_CI.headland = OAA_CI.headland || {};

OAA_CI.headland.treeView      = new OAA_CI.SidebarView('ID_TREECHILDREN_HEADINGS_LANDMARKS');
OAA_CI.headland.mainView      = new OAA_CI.SidebarView('ID_TREECHILDREN_MAIN');
OAA_CI.headland.contentView = new OAA_CI.SidebarView('ID_TREECHILDREN_CONTENT');

OAA_CI.headland.views = [
  OAA_CI.headland.treeView,
  OAA_CI.headland.mainView,
  OAA_CI.headland.contentView
];

OAA_CI.headland.hideMeter = function () { OAA_CI.headland.meter.hidden = true; };
OAA_CI.headland.showMeter = function () { OAA_CI.headland.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.headland;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */

OpenAjax.a11y.cache.HeadingElement.prototype.getDisplayProperties = function (id) {
    return [this.dom_element.tag_name, this.level, this.name, this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.LandmarkElement.prototype.getDisplayProperties = function (id) {
    return [this.dom_element.tag_name, this.dom_element.role, this.label, this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.MainElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.dom_element.role, this.label, this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.H1Element.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.level, this.name, this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.TitleElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.level, this.name, this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.DOMElement.prototype.getDisplayProperties = function () {
  return [this.tag_name, "", this.getAccessibility()];
};

OpenAjax.a11y.cache.DOMText.prototype.getDisplayProperties = function () {
  return [this.parent_element.tag_name, this.text, this.getAccessibility()];
};

/**
 * getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 *       populateTreeAsHierarchy function.
 *
 * @return List of child elements
 */

OpenAjax.a11y.cache.HeadingElement.prototype.getChildren = function () {
  return [];
};

OpenAjax.a11y.cache.LandmarkElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.MainElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

/**
 * onLoad
 *
 * @desc Initialize Headings/Landmarks sidebar.
 *
 * @return nothing
 */

OAA_CI.headland.onLoad = function(oaa_cache_inspector) {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.headland.treeView.addProperties(pt);  
  OAA_CI.headland.mainView.addProperties(pt);  
  OAA_CI.headland.contentView.addProperties(pt);  

  OAA_CI.headland.meter = document.getElementById('ID_METER_HEADINGS_LANDMARKS');

  OAA_CI.headland.treeView.isInitialized = false;
  OAA_CI.headland.treeView.showCacheItems();

};

/**
 * @function treeView.showCacheItems
 *
 * @memberOf OAA_CI.headland
 *
 * @desc Respond to the Tree View button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with list
 *       elements structured as a hierarchical list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.headland.treeView.showCacheItems = function () {
  var view = OAA_CI.headland.treeView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_HEADINGS_LANDMARKS');
    view.tree  = document.getElementById('ID_TREE_HEADINGS_LANDMARKS');
    view.cache = OAA_CI.ruleset.dom_cache.headings_landmarks_cache;

    // Populate the tabpanel
    var child_elements = view.cache.child_cache_elements;
    OAA_CI.populateTreeAsHierarchy(child_elements, 'ID_TREECHILDREN_HEADINGS_LANDMARKS');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function populateTitleH1Main
 *
 * @memberOf OAA_CI.headland
 *
 * @desc Populate the XUL textbox and tree containing title, h1 and main elements.
 */

OAA_CI.headland.populateTitleH1Main = function() {

  function addMainChildren (xul_treechildren, main_elements) {

    var max = main_elements.length;
    var xul_treeitem;
    var xul_sub_treechildren;

    for (var i = 0; i < max; i++) {

      var me = main_elements[i];
      xul_treeitem = document.createElement('treeitem');

      if (me.main_type == OpenAjax.a11y.MAIN.H1_ELEMENT) {
        OAA_CI.addTreeRow(xul_treeitem, [me.dom_element.tag_name, "1", me.name, me.dom_element.getAccessibility()], me.cache_id );
      }
      else {
        if (me.main_type == OpenAjax.a11y.MAIN.TITLE_ELEMENT) {
          OAA_CI.addTreeRow(xul_treeitem, [me.dom_element.tag_name, "", me.name, me.dom_element.getAccessibility()], me.cache_id );
        }
        else {
          if (me.main_type == OpenAjax.a11y.MAIN.BODY_ELEMENT) {
            OAA_CI.addTreeRow(xul_treeitem, [me.dom_element.tag_name, "", "", me.dom_element.getAccessibility()], me.cache_id );
          }
          else {
            OAA_CI.addTreeRow(xul_treeitem, [me.dom_element.tag_name, me.dom_element.role, me.label, me.dom_element.getAccessibility()], me.cache_id );

            if (me.child_cache_elements.length) {
              xul_treeitem.setAttribute("container", "true");
              xul_treeitem.setAttribute("open", "true");

              xul_sub_treechildren = document.createElement('treechildren');
              addMainChildren(xul_sub_treechildren, me.child_cache_elements);
              xul_treeitem.appendChild(xul_sub_treechildren);
            }
          }  
        }
      }
      xul_treechildren.appendChild(xul_treeitem);

    } // end for

  } // end function

  var title_main_cache = OAA_CI.ruleset.dom_cache.title_main_cache;
  var child_main_elements = title_main_cache.child_cache_elements;

  var start_time = new Date().getTime();

  var xul_title_textbox = document.getElementById('ID_TITLE_TEXTBOX');
  xul_title_textbox.value = OAA_CI.ruleset.dom_cache.document.title;

  var xul_treechildren = document.getElementById('ID_TREECHILDREN_MAIN');
  OAA_CI.removeChildNodes(xul_treechildren);
  addMainChildren(xul_treechildren, child_main_elements);

  OAA_CI.updateXULTime(start_time);

};

/**
 * @function mainView.showCacheItems
 *
 * @memberOf OAA_CI.headland
 *
 * @desc Respond to the Title/Main/H1 button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with various
 *       elements structured as a hierarchical list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.headland.mainView.showCacheItems = function () {
  var view = OAA_CI.headland.mainView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_MAIN');
    view.tree  = document.getElementById('ID_TREE_MAIN');
    view.cache = OAA_CI.ruleset.dom_cache.title_main_cache;

    // Populate the tabpanel
    OAA_CI.headland.populateTitleH1Main();
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};


/**
 * @function contentView.showCacheItems
 *
 * @memberOf OAA_CI.headland
 *
 * @desc Respond to the No Landmarks button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with list
 *       elements structured as a hierarchical list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.headland.contentView.showCacheItems = function () {
  var view = OAA_CI.headland.contentView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_CONTENT');
    view.tree  = document.getElementById('ID_TREE_CONTENT');
    view.cache = OAA_CI.ruleset.dom_cache.headings_landmarks_cache;
    view.alt_cache = OAA_CI.ruleset.dom_cache.headings_landmarks_cache;

    // Populate the tabpanel
    var dom_elements = view.cache.elements_with_content;
    OAA_CI.populateTreeAsFlatList(dom_elements, 'ID_TREECHILDREN_CONTENT');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.headland
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.headland.onItemSelectionChange = function (index) {

  var view = OAA_CI.headland.views[index];
  view.updateItemSelectionDisplay();

};
